package com.demo.las.controller;

import com.demo.las.dto.ApiResponse;
import com.demo.las.entity.StudentStudyRecord;
import com.demo.las.service.StudentStudyRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/api/learning-records")
@CrossOrigin(origins = "*")
public class StudentStudyRecordController {

    @Autowired
    private StudentStudyRecordService service;

    @PostMapping
    public ApiResponse<StudentStudyRecord> create(@RequestParam("studentName") String studentName,
                                                  @RequestParam("studentNo") String studentNo,
                                                  @RequestParam("courseName") String courseName,
                                                  @RequestParam("knowledgePoint") String knowledgePoint,
                                                  @RequestParam("startTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
                                                  @RequestParam("durationMinutes") Integer durationMinutes) {
        if (durationMinutes == null || durationMinutes <= 0) {
            return ApiResponse.error(400, "学习时长必须为正整数（分钟）");
        }
        StudentStudyRecord saved = service.create(studentName, studentNo, courseName, knowledgePoint, startTime, durationMinutes);
        return ApiResponse.success("创建成功", saved);
    }

    @GetMapping
    public ApiResponse<List<StudentStudyRecord>> list(@RequestParam(value = "studentName", required = false) String studentName,
                                                      @RequestParam(value = "studentNo", required = false) String studentNo,
                                                      @RequestParam(value = "courseName", required = false) String courseName,
                                                      @RequestParam(value = "knowledgePoint", required = false) String knowledgePoint) {
        List<StudentStudyRecord> records = service.list(studentName, studentNo, courseName, knowledgePoint);
        return ApiResponse.success(records);
    }

    @GetMapping("/overview")
    public ApiResponse<List<StudentStudyRecordService.OverviewItem>> overview() {
        return ApiResponse.success(service.overview());
    }
}